@isTest

/*
 test class on TriggerDateISV
 by hxj 20101216
*/
private class TestTriggerDateISV
{
   static testMethod void run()
   {
        Profile p=[select  id from profile where  name='Sales Rep - Trade' limit 1];
        User u=new user(alias='hxj',email='xjhuang807@isoftstone.com',lastName='testing'
        ,languageLocaleKey='zh_CN',localeSidkey='zh_CN' ,TimeZoneSidKey='Asia/Shanghai',EmailEncodingKey='UTF-8',
        profileId=p.id,userName='xjhuang123@isoftstone.com');
              
        System.runAs(u)
        {
                    
           //新建地区
          Place__c oPlace=new Place__c();
          oPlace.name='地区1';
          oPlace.CityType__c='Base City';
          oPlace.PlaceType__c='City';
          oPlace.CityCode__c='c001';
          insert oPlace;
          
          //新建店面
          Account oAcc=new   Account();
          oAcc.Name='店面1';
          oAcc.City__c=oPlace.id;
          oAcc.Channel__c='百货店';
          oAcc.Contact__c='aaa';
          oAcc.No_of_checkout__c=22;
          oAcc.ContactPhoneNumber__c='1234567456';
          insert oAcc;
          
           //拜访计划       
           //今天
          Routing_Plan_And_Visit__c oRPAV=new  Routing_Plan_And_Visit__c();
          oRPAV.Visit_Date__c=date.today();
          insert oRPAV;
          //前天
          Routing_Plan_And_Visit__c oRPAVYes=new  Routing_Plan_And_Visit__c();
          oRPAVYes.Visit_Date__c=date.today().addDays(-2);
          insert oRPAVYes;
          //上个月今天
          Routing_Plan_And_Visit__c oRPAVLM=new  Routing_Plan_And_Visit__c();
          oRPAVLM.Visit_Date__c=date.today().addMonths(-1);
          insert oRPAVLM;
          //计划
          RoutingPlan__c oRP=new RoutingPlan__c();
          oRP.RoutingPlanAndVisit__c=oRPAV.Id;
          oRP.OutletName__c=oAcc.Id;
          oRP.StartDateTime__c=dateTime.now();
          oRP.EndDateTime__c=dateTime.now().addHours(4);
          insert oRP;
          
           //新建instoreVisit记录
            list< In_Store_Visit__c> lstISV=new  list< In_Store_Visit__c>();
            
            //今天
            In_Store_Visit__c oISV1=new  In_Store_Visit__c();
            oISV1.RoutingPlanAndVisit__c=oRPAV.id;
            oISV1.Start_Time__c=dateTime.now();
            oISV1.End_Time__c=dateTime.now().addHours(1);
            oISV1.OutletName_del__c=oAcc.id;
            lstISV.add(oISV1);
            //昨天
             In_Store_Visit__c oISV5=new  In_Store_Visit__c();
            oISV5.RoutingPlanAndVisit__c=oRPAV.id;
            oISV5.Start_Time__c=dateTime.now().addDays(-1);
            oISV5.End_Time__c=dateTime.now().addHours(1).addDays(-1);
            oISV5.OutletName_del__c=oAcc.id;
            lstISV.add(oISV5);
            //前天
            In_Store_Visit__c oISV2=new  In_Store_Visit__c();
            oISV2.RoutingPlanAndVisit__c=oRPAVYes.id;
            oISV2.Start_Time__c=dateTime.now().addDays(-2);
            oISV2.End_Time__c=dateTime.now().addDays(-2).addHours(1);
            oISV2.OutletName_del__c=oAcc.id;
            oISV2.RoutingPlanItem__c=oRP.id;
            lstISV.add(oISV2);
            //上个月今天
             In_Store_Visit__c oISV3=new  In_Store_Visit__c();
            oISV3.RoutingPlanAndVisit__c=oRPAVLM.id;
            oISV3.Start_Time__c=dateTime.now().addMonths(-1);
            oISV3.End_Time__c=dateTime.now().addMonths(-1).addHours(1);
            oISV3.OutletName_del__c=oAcc.id;
            oISV3.RoutingPlanItem__c=oRP.id;
            lstISV.add(oISV3);
            //上个月昨天
             In_Store_Visit__c oISV4=new  In_Store_Visit__c();
            oISV4.RoutingPlanAndVisit__c=oRPAVLM.id;
            oISV4.Start_Time__c=dateTime.now().addMonths(-1).addDays(-1);
            oISV4.End_Time__c=dateTime.now().addMonths(-1).addDays(-1).addHours(1);
            oISV4.OutletName_del__c=oAcc.id;
            oISV4.RoutingPlanItem__c=oRP.id;
            lstISV.add(oISV4);
                     
           insert lstISV;
           
           //更新昨天的分销，并手工赋值对应分销日期
            Inventory__c oInv=new   Inventory__c ( In_Store_Visit__c=oISV5.id);
            oInv.Inventory__c=12;
            insert oInv;
            oISV5.LastDistributionDate__c=oISV5.StartDate__c;
            update oISV5;
            //更新上个月昨天的分销，并手工赋值对应分销日期
            Inventory__c oInv1=new   Inventory__c ( In_Store_Visit__c=oISV4.id);
            oInv1.Inventory__c=13;
            insert oInv1;
            oISV4.LastDistributionDate__c=oISV4.StartDate__c;
            update oISV4;
            
            //更新其他拜访记录的分销
            Inventory__c oInv3=new   Inventory__c ( In_Store_Visit__c=oISV1.id);
            oInv3.Inventory__c=12;
            insert oInv3;
           
            
            Inventory__c oInv4=new   Inventory__c ( In_Store_Visit__c=oISV2.id);
            oInv4.Inventory__c=12;
            insert oInv4;
           
            
            Inventory__c oInv5=new   Inventory__c (In_Store_Visit__c=oISV3.id);
            oInv5.Inventory__c=12;
            insert oInv5;
          
            
            set<id> ids=new set<id>();
            for(In_Store_Visit__c i:lstISV)
            {
               ids.add(i.id);
            }
            
            //调用被测试的class
            TriggerDateISV.invokeByTrigger(ids);
            
            //检查分销日期是否正确更新
            list<In_Store_Visit__c> lIsvRet=[select id,LastDistributionDate__c from In_Store_Visit__c where id in:ids];
            for(In_Store_Visit__c i:lIsvRet)
            {
               //本月
               if(i.id==oISV1.id)
               {
                   system.assert(i.LastDistributionDate__c!=null,'error:::,这个记录的分销日期为空');
               }
               if(i.id==oISV2.id)
               {
                   system.assert(i.LastDistributionDate__c==null,'error:::,这个记录的分销日期不为空');
               }
               if(i.id==oISV5.id)
               {
                   system.assert(i.LastDistributionDate__c==null,'error:::,这个记录的分销日期不为空');
               }
               //上个月
                if(i.id==oISV3.id)
               {
                   system.assert(i.LastDistributionDate__c!=null,'error:::,这个记录的分销日期为空');
               }
               if(i.id==oISV4.id)
               {
                   system.assert(i.LastDistributionDate__c==null,'error:::,这个记录的分销日期不为空');
               }
            }
           
        }
   }
}